﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using DalFactory;
using IDal;

namespace Bll
{
    public abstract partial class BaseBll<T> where T : class,new()
    {
        public IBaseDal<T> CurrentDal { get; set; }
        public abstract void SetCurrentDal();//抽象方法：要求子类必须实现
        public IDalSession CurrentDalSession
        {
            get
            {
                return DalSessionUtil.GetCurrentDalSession();
            }
        }

        public BaseBll()
        {//父类的构造函数，会在子类创建对象时被调用
            SetCurrentDal();
        }

        #region 增加
        public T Add(T entity)
        {
            CurrentDal.Add(entity);
            CurrentDalSession.SaveChanges();
            return entity;
        }
        #endregion

        #region 删除
        public bool Delete(T entity)
        {
            CurrentDal.Delete(entity);
            return CurrentDalSession.SaveChanges() > 0;
        }
        #endregion

        #region 修改
        public bool Update(T entity)
        {
            CurrentDal.Update(entity);
            return CurrentDalSession.SaveChanges() > 0;
        }
        #endregion

        #region 一般查询
        public IQueryable<T> GetEntities(Expression<Func<T, bool>> whereLambda)
        {
            return CurrentDal.GetEntities(whereLambda);
        }
        #endregion

        #region 分页查询
        public IQueryable<T> GetEntitiesByPage<S>(int pageSize, int pageIndex, out int total, Expression<Func<T, bool>> whereLambda, Expression<Func<T, S>> orderbyLambda, bool isAsc)
        {
            return CurrentDal.GetEntitiesByPage<S>(pageSize, pageIndex, out total, whereLambda, orderbyLambda, isAsc);

        }
        #endregion
    }
}
